package com.wantupai.translate.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.Map;

/**
 * Author : luxuchang
 * Time : 2018/8/18
 * Description :
 */
public class SspQqUtils {

    private static volatile SspQqUtils instance;
    private Context context;

    public static SspQqUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (SspQqUtils.class) {
                if (instance == null)
                    instance = new SspQqUtils(context.getApplicationContext());
            }
        }
        return instance;
    }

    public SspQqUtils(Context context) {
        this.context = context;
    }

    public void shareImgAndWords(Activity activity, String url, String imgUrl, String title, String content, final ShareOnResult shareOnResult) {

        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        //这条分享消息被好友点击后的跳转URL。
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        //分享的标题。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_	SUMMARY不能全为空，最少必须有一个是有值的。
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        //分享的图片URL
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imgUrl);
        //分享的消息摘要，最长50个字
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);
        //手Q客户端顶部，替换“返回”按钮文字，如果为空，用返回代替
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "万图拍");

        getTencent().shareToQQ(activity, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                Log.e("share", "onComplete: " );
                if (shareOnResult != null)
                    shareOnResult.onSuccess();
            }

            @Override
            public void onError(UiError uiError) {
                Log.e("share", "onError: " );

                if (shareOnResult != null)
                    shareOnResult.onFail(uiError.errorDetail);
            }

            @Override
            public void onCancel() {
                Log.e("share", "onCancel: " );
                if (shareOnResult != null)
                    shareOnResult.onFail("分享失败");
            }
        });
    }


    public void shareImage(final Activity activity, String path, final ShareOnResult shareOnResult) {
        Bundle params = new Bundle();
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, path);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "万图拍");
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.
                SHARE_TO_QQ_TYPE_IMAGE);
//        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.
//                SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);

        getTencent().shareToQQ(activity, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                if (shareOnResult != null)
                    shareOnResult.onSuccess();
            }

            @Override
            public void onError(UiError uiError) {
                if (shareOnResult != null)
                    shareOnResult.onFail(uiError.errorDetail);
            }

            @Override
            public void onCancel() {
                if (shareOnResult != null)
                    shareOnResult.onFail("分享失败");
            }
        });
    }


    public void shareTxt(Activity activity, String txt, final ShareOnResult shareOnResult) {
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的标题");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://www.qq.com/news/1.html");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "测试应用222222");
        params.putString(QQShare.SHARE_TO_QQ_ARK_INFO, "1111111111111111111111111111111111111111111111");
        getTencent().shareToQQ(activity, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                if (shareOnResult != null)
                    shareOnResult.onSuccess();
            }

            @Override
            public void onError(UiError uiError) {
                if (shareOnResult != null)
                    shareOnResult.onFail(uiError.errorDetail);
            }

            @Override
            public void onCancel() {
                if (shareOnResult != null)
                    shareOnResult.onFail("分享失败");
            }
        });
    }


    public void unLogin(Context context) {
        getTencent().logout(context);
    }

    public Tencent getTencent() {
        return Tencent.createInstance("101465687", context);
    }

    public interface ShareOnResult {
        void onSuccess();

        void onFail(String error);
    }

}
